﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace miSuperEngine
{
    public class Camera
    {

        Vector3 position;
        Vector3 direction;
        Vector3 up;
        Vector3 lookAtVec;
        Vector3 right;
        Actor actorToFollow;
        float fieldOfView;
        int nearPlane;
        int farPlane;
        public Matrix projection, view;
        public Camera(Vector3 pos, Vector3 look,Vector3 _up)
        {
            lookAtVec = look;
            position = pos;
            up = _up;
            direction = lookAtVec - position;
            direction.Normalize();
            fieldOfView = MathHelper.ToRadians(45);
            nearPlane=1 ;
            farPlane = 10000;
            right=Vector3.Cross(direction, up);
            projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, SceneManager.screenSize.X / SceneManager.screenSize.Y, nearPlane, farPlane);
            view = Matrix.CreateLookAt(pos, look, _up);
        }
        public Vector3 getPosition()
        {
            return position;
        }
        public Vector3 getDirection()
        {
            return direction;
        }
        public Vector3 getUp()
        {
            return up;
        }
        public Vector3 getLookAtVec()
        {
            return lookAtVec;
        }
        public Vector3 getRight()
        {
            return right;
        }
        public void setPosition(Vector3 pos)
        {
            position=pos;
        }
        public void setDirection(Vector3 dir)
        {
             direction=dir;
        }
        public void setUp(Vector3 _up)
        {
            up=_up;
        }
        public void setLookAt(Vector3 la)
        {
            lookAtVec=la;
        }
        public void setRight(Vector3 rgt)
        {
            right=rgt;
        }
        void recalcMatrixes()
        {
            //projection = Matrix.CreatePerspectiveFieldOfView(fieldOfView, SceneManager.screenSize.X / SceneManager.screenSize.Y, nearPlane, farPlane);
            view = Matrix.CreateLookAt(position, position+direction, up);
        }
        public void moveForward(float distance)
        {
            position += direction*distance;
            recalcMatrixes();
        }
        public void strafe(float distance)
        {
            position += right*distance;
            recalcMatrixes();
        }
        public void rotateY(float rotY)
        {
            Matrix rotation = Matrix.CreateRotationY(rotY);
            direction=Vector3.Transform(direction, rotation);
            right = Vector3.Transform(right, rotation);
            lookAtVec = Vector3.Transform(lookAtVec, rotation);
            up = Vector3.Transform(up, rotation);
            recalcMatrixes();
        }
        /*public void rotateX(float rotX)
        {
            Matrix rotation = Matrix.CreateRotationX(rotX);
            direction = Vector3.Transform(direction, rotation);
            right = Vector3.Transform(right, rotation);
            lookAt = Vector3.Transform(lookAt, rotation);
            up = Vector3.Transform(up, rotation);
        }*/


    }
}
